package zmee.pmas.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import zmee.common.allhelper.OccupiedAmountCalculateHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

/**
 * @author yss
 * @createTime 2022-12-07 14:26
 * @description 项目信息调整审批扩展插件
 */
public class ProApprovalAdjustAuditOp extends AbstractOperationServicePlugIn {


    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        String key = e.getOperationKey();
        for (DynamicObject dataEntry : dataEntities) {
            // 2023-9-5 隐藏该段逻辑
//            if(!dataEntry.getDynamicObjectType().isForceUpdateModifier()) {
//                dataEntry = BusinessDataServiceHelper.loadSingle(dataEntry.getPkValue(), dataEntry.getDynamicObjectType().getName());
//            }
            dataEntry = BusinessDataServiceHelper.loadSingle(dataEntry.getPkValue(), dataEntry.getDynamicObjectType().getName());
            String combofield = dataEntry.getString("zmee_combofield");
            if("02".equals(combofield)) {
                if ("submit".equals(key)) {
                    checkProjectTotalAmt(dataEntry, e);
                }
                if ("audit".equals(key)) {
                    updateProjcetBugamt(dataEntry);
                }
            }

        }
    }

    /**
     * 校验调整金额
     * @param dataEntry
     * @param e
     */
    private void checkProjectTotalAmt(DynamicObject dataEntry, BeforeOperationArgs e){
        String adjustTypeStr = dataEntry.getString("zmee_combofield");
        if("02".equals(adjustTypeStr)){//项目概算总额调整
            DynamicObject projectDy = dataEntry.getDynamicObject("pro");
            if(projectDy == null){
                e.setCancelMessage("项目名称不允许为空");
                e.setCancel(true);
                return;
            }
            long projectPk = projectDy.getLong("id");
            //已占用项目概算总额
            BigDecimal allOccupiedByProjectAmt = OccupiedAmountCalculateHelper.getAllOccupiedByProject(projectPk);
            //项目概算总额
            BigDecimal adjustProTotalAmt = dataEntry.getBigDecimal("zmee_decimalfield");
            DynamicObject kind = dataEntry.getDynamicObject("kind");
            String number = kind.getString("number");
            String parentNumber = kind.getString("parent.number");
            if(adjustProTotalAmt.compareTo(allOccupiedByProjectAmt) < 0){
                e.setCancelMessage("调整后的项目概算总额小于项目已占用金额");
                e.setCancel(true);
            }
        }
    }

    private void updateProjcetBugamt(DynamicObject dataEntry){
        //写回项目立项单据信息
        //获取立项单信息
        DynamicObject projectDy = dataEntry.getDynamicObject("pro");
        if(projectDy == null){
            return;
        }
        long projectPk = projectDy.getLong("id");
        BigDecimal adjustProTotalAmt = dataEntry.getBigDecimal("zmee_decimalfield");
        DynamicObject approvalDy = BusinessDataServiceHelper.loadSingleFromCache("pmas_pro_approval", new QFilter[]{new QFilter("pro.id", QCP.equals, projectPk)});
        approvalDy.set("projcetbugamt",adjustProTotalAmt);
        SaveServiceHelper.update(approvalDy);
    }
}
